class server {
    constructor() {
        this.routerCount=0;
    }
    //
    commonServer() {
        return new Promise(res => {
            util.ajax({
                url: config.baseApi + '',
                success: (data => {
                    
                })
            })
        })
    };
    addShoppingCar(itemCode) {
        return new Promise(res => {
            util.ajax({
                url: config.baseApi + 'bookMall-h5/api/car/addShoppingCar',
                data:{
                    itemCode: itemCode
                },
                success: (data => {
                    popup.miss({content:'添加成功！'});
                    Vue.$store.commit('cart/RPODUCT_ADD', itemCode)  
                    res();
                })
            })
        })
    };

    getShoppingCarList() {
        return new Promise(res => {
            util.ajax({
                url: config.baseApi + 'bookMall-h5/api/car/getShoppingCarList',
                success: (data => {
                    res(data)
                })
            })
        })
    };

    getUserInfo(login,route){
        let userinfo = util.getStorage('local','userinfo');
        if (userinfo && JSON.parse(userinfo).openid && !login){
            if (this.routerCount === 0) {
                this.getShoppingCarList().then(res => {
                    res.data.forEach((item, i) => {
                        Vue.$store.commit('cart/RPODUCT_ADD', item.itemCode)  
                    })
                });
                this.routerCount++;
            }
            return;
        }

        let code = util.getQueryString('code')
        if (!code){
            // alert(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${config.appId}&redirect_uri=${encodeURIComponent(config.redirect_uri)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`)
            location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${config.appId}&redirect_uri=${encodeURIComponent(config.redirect_uri)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
            // location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${config.appId}&redirect_uri=${encodeURIComponent(config.redirect_uri)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
            return;
        }

        return new Promise((resolve,reject)=>{
            util.ajax({
                url: config.baseApi + 'bookMall-h5/api/common/getWxUserInfoByCode',
                data: {
                    code: code,
                    companyCode: route.query.companyCode || ''
                },
                success: (data => {
                    util.setStorage('local', 'userinfo', data.data);
                    resolve();
                })
            })
        })
    };

    queryPayStatus(orderNo){
        return new Promise((resolve, reject) => {
            util.ajax({
                url: config.baseApi + 'bookMall-h5/api/order/queryPayStatus',
                data: {
                    orderNo: orderNo,
                },
                success: (data => {
                    resolve(data.data);
                })
            })
        })
    }

    getAddressInfo(parentAddressId){
        return new Promise(res=>{
            let param = {
                parentAddressId: parentAddressId || 0
            }
            util.ajax({
                url: config.baseApi + 'bookMall-h5/api/userAddress/getAddressInfo',
                data: param,
                success:(data=>{
                    res(data.data);
                })
            })
        })
    }
}
window['server'] = new server();